#!/bin/bash
#
# pack/pack
# (c) Copyright 2013
# Allwinner Technology Co., Ltd. <www.allwinnertech.com>
# flord wang <wangflord@allwinnertech.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

function pack_error()
{
    echo -e "\033[47;31mERROR: $*\033[0m"
}

function pack_warn()
{
    echo -e "\033[47;34mWARN: $*\033[0m"
}

function pack_info()
{
    echo -e "\033[47;30mINFO: $*\033[0m"
}

show_help()
{
	printf "\nbuild.sh - parser firmware and fetch one file\n"
	echo " this script is used to create all keys defined in the dragon_toc.cfg"
	printf "\n"
}

createkeys()
{
	printf "ready to create keys\n"

	cp -v ${TOP_DIR}/out/${PACK_BOARD}/image/dragon_toc.cfg dragon_toc.cfg

	if [ $? -ne 0 ]
	then
		pack_error "dragon toc config file is not exist"
		exit 1
	fi

	dragonsecboot -key dragon_toc.cfg ${TOP_DIR}/out/${PACK_BOARD}/keys
	if [ $? -ne 0 ]
	then
		pack_error "dragon toc run error"
		rm -rf dragon_toc.cfg

		exit 1
	fi

	rm -rf dragon_toc.cfg
}

while getopts b:t:m:s: OPTION
do
	case $OPTION in
	t) TOP_DIR=$OPTARG
	;;
	b) PACK_BOARD=$OPTARG
	;;
	*) show_help
	exit 0
	;;
esac
done

HOST_DIR=${TOP_DIR}/out/host/bin

export PATH=${HOST_DIR}:$PATH

createkeys
